package com.daniel.cms.config;

import com.daniel.cms.utils.ResultEnum;
import com.daniel.cms.utils.ResultVO;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 详细说明
 *
 * @author Daniel
 * @version V1.0
 * @date 2020/5/22 15:22
 */
@RestControllerAdvice
public class ExceptionControllerAdvice {

	@ExceptionHandler(MethodArgumentNotValidException.class)
	public ResultVO MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
		// 从异常对象中拿到ObjectError对象
		ObjectError objectError = e.getBindingResult().getAllErrors().stream().findFirst().get();
		// 然后提取错误提示信息进行返回
		return new ResultVO(ResultEnum.VALID, objectError.getDefaultMessage());
	}
}
